home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_069 / blitlab / math.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  165 lines

  1. /*
  2.  *   This is the math routines of BlitLab.  It checks a possible blit to
  3.  *   insure that it is safe.  It also handles the line calculations.
  4.  */
  5. #include "structures.h"
  6. /*
  7.  *   The externals we use.
  8.  */
  9. extern long gvals[] ;
  10. extern struct Gadget *gadgets[] ;
  11. extern short *realbits ;
  12. extern struct Window *mywindow ;
  13. extern char errorbuf[] ;
  14. /*
  15.  *   This routine insures that a blit is safe.  It returns 1 if it is
  16.  *   okay, and 0 if it is not.
  17.  */
  18. int blitsafe() {
  19.    long x1, x2, x3, x4 ;
  20.    long lower, upper ;
  21.  
  22.    if (gvals[GDGUSED]==0)
  23.       return(1) ;
  24.    lower = (long)realbits ;
  25.    upper = 382 + (long)realbits ;
  26.    if (gvals[GDGH] < 1 || gvals[GDGV] < 1
  27.        || gvals[GDGH] > 64 || gvals[GDGV] > 1024)
  28.       return(0) ;
  29.    if (gvals[GDGLINE]) {
  30.    } else {
  31.       x1 = gvals[GDGDPT] ;
  32.       if (gvals[GDGDESC]) {
  33.          x2 = x1 - gvals[GDGH] * 2 + 2 ;
  34.          x3 = x1 - (gvals[GDGV] - 1) * ((gvals[GDGH] * 2) +
  35.                     (gvals[GDGDMOD] & ~1)) ;
  36.          x4 = x3 - gvals[GDGH] * 2 + 2 ;
  37.          if (x1 < lower || x2 < lower || x3 < lower || x4 < lower ||
  38.              x1 > upper || x2 > upper || x3 > upper || x4 > upper)
  39.             return(0) ;
  40.          else
  41.             return(1) ;
  42.       } else {
  43.          x2 = x1 + gvals[GDGH] * 2 - 2 ;
  44.          x3 = x1 + (gvals[GDGV] - 1) * ((gvals[GDGH] * 2) + 
  45.                         (gvals[GDGDMOD] & ~1)) ;
  46.          x4 = x3 + gvals[GDGH] * 2 - 2 ;
  47.          if (x1 < lower || x2 < lower || x3 < lower || x4 < lower ||
  48.              x1 > upper || x2 > upper || x3 > upper || x4 > upper)
  49.             return(0) ;
  50.          else
  51.             return(1) ;
  52.       }
  53.    }
  54. }
  55. /*
  56.  *   This routine stuffs a value in a gadget.  Could be dangerous, but
  57.  *   that's life.
  58.  */
  59. stuff(id, s)
  60. int id ;
  61. char *s ;
  62. {
  63.    gvals[id] = parse(s) ;
  64.    RemoveGadget(mywindow, gadgets[id]) ;
  65.    strcpy(((struct StringInfo *)(gadgets[id]->SpecialInfo))->Buffer, s) ;
  66.    AddGadget(mywindow, gadgets[id], -1) ;
  67.    RefreshGadgets(gadgets[id], mywindow, NULL) ;
  68. }
  69. /*
  70.  *   This routine flips the state of a toggle gadget.
  71.  */
  72. flipgadg(id)
  73. int id ;
  74. {
  75.    struct IntuiText *temp ;
  76.    struct Gadget *gp = gadgets[id] ;
  77.  
  78.    RemoveGadget(mywindow, gp) ;
  79.    temp = gp->GadgetText ;
  80.    gp->GadgetText = (struct IntuiText *)gp->UserData ;
  81.    gp->UserData = (APTR)temp ;
  82.    gp->NextGadget = NULL ;
  83.    AddGadget(mywindow, gp, -1) ;
  84.    RefreshGadgets(gp, mywindow, NULL) ;
  85.    gvals[id] = 1 - gvals[id] ;
  86. }
  87. /*
  88.  *   This routine sets up a line.
  89.  */
  90. setupline() {
  91.    int x, y ;
  92.    int i ;
  93.    int X, Y ;
  94.    int q = 0 ;
  95.  
  96.    parseall() ;
  97.    stuff(GDGADAT, "$8000") ;
  98.    stuff(GDGBDAT, "$ffff") ;
  99.    x = gvals[GDGSX] ;
  100.    y = gvals[GDGSY] ;
  101.    sprintf(errorbuf, "%d", x & 15) ;
  102.    stuff(GDGASH, errorbuf) ;
  103.    i = ((x >> 3) & ~1) + y * 12 ;
  104.    sprintf(errorbuf, "M+%d", i) ;
  105.    stuff(GDGCPT, errorbuf) ;
  106.    stuff(GDGDPT, errorbuf) ;
  107.    stuff(GDGCMOD, "12") ;
  108.    stuff(GDGDMOD, "12") ;
  109.    stuff(GDGH, "2") ;
  110.    x = (gvals[GDGEX] - gvals[GDGSX]) ;
  111.    y = (gvals[GDGEY] - gvals[GDGSY]) ;
  112.    if (x < 0)
  113.       X = - x ;
  114.    else
  115.       X = x ;
  116.    if (y < 0)
  117.       Y = - y ;
  118.    else
  119.       Y = y ;
  120.    if (x > 0) {
  121.       if (y > 0) {
  122.          q = (X > Y ? 1 : 0) ;
  123.       } else {
  124.          q = (X > Y ? 3 : 4) ;
  125.       }
  126.    } else {
  127.       if (y > 0) {
  128.          q = (X > Y ? 5 : 2) ;
  129.       } else {
  130.          q = (X > Y ? 7 : 6) ;
  131.       }
  132.    }
  133.    if (Y > X) {
  134.       i = X ;
  135.       X = Y ;
  136.       Y = i ;
  137.    }
  138.    sprintf(errorbuf, "%d", X+1) ;
  139.    stuff(GDGV, errorbuf) ;
  140.    sprintf(errorbuf, "%d", 2 * Y - X) ;
  141.    stuff(GDGAPT, errorbuf) ;
  142.    sprintf(errorbuf, "%d", 4 * (Y - X)) ;
  143.    stuff(GDGAMOD, errorbuf) ;
  144.    sprintf(errorbuf, "%d", 4 * Y) ;
  145.    stuff(GDGBMOD, errorbuf) ;
  146.    stuff(GDGAFWM, "%1111111111111111") ;
  147.    stuff(GDGALWM, "%1111111111111111") ;
  148.    if (! gvals[GDGLINE])
  149.       flipgadg(GDGLINE) ;
  150.    if ((q & 1) != gvals[GDGEFE])
  151.       flipgadg(GDGEFE) ;
  152.    if (((q >> 1) & 1) != gvals[GDGIFE])
  153.       flipgadg(GDGIFE) ;
  154.    if (((q >> 2) & 1) != gvals[GDGFCI])
  155.       flipgadg(GDGFCI) ;
  156.    if (! gvals[GDGUSEA])
  157.       flipgadg(GDGUSEA) ;
  158.    if (gvals[GDGUSEB])
  159.       flipgadg(GDGUSEB) ;
  160.    if (! gvals[GDGUSEC])
  161.       flipgadg(GDGUSEC) ;
  162.    if (! gvals[GDGUSED])
  163.       flipgadg(GDGUSED) ;
  164. }
  165.